﻿#include <QStyleFactory>
#include <QMenu>
#include <QFile>
#include "CBeltCorrectMain.h"
#include "CGlobal.h"
#include "Public.h"
#include "CCorrectResult.h"
#include "CCorrectPlcOper.h"
#include "CLogOper.h"
#include "CCorrectDevCfgDlg.h"
#include "CVideoLargeDlg.h"

CBeltCorrectMain::CBeltCorrectMain(CDevBeltRealign* pDev)
	:m_pDev(pDev)
{
	ui.setupUi(this);
	m_bIsPlaying = false;
	//setStyleSheetFile();
	initConnect();
	initUi();
}

CBeltCorrectMain::~CBeltCorrectMain()
{
	if (m_bIsPlaying)
	{
		endPlayVideo();
	}
}


void CBeltCorrectMain::closeEvent(QCloseEvent *event)
{
	if (m_bIsPlaying)
	{
		endPlayVideo();
	}
}

void CBeltCorrectMain::showEvent(QShowEvent *event)
{
	if (!m_bIsPlaying)
	{
		startPlayVideo();
	}
	//确定纠偏高级功能按钮的显示和隐藏
	if (m_pDev->m_pDevData->m_nIsHaveDev == 1)
	{
		ui.pushButtonCorrectPlc->show();
	}
	else if (m_pDev->m_pDevData->m_nIsHaveDev == 0)
	{
		ui.pushButtonCorrectPlc->hide();
	}
}

void CBeltCorrectMain::changeEvent(QEvent *event)
{
	if (event->type() == QEvent::WindowStateChange)
	{
		if (isMinimized())
		{
			if (m_bIsPlaying)
			{
				endPlayVideo();
			}
		}
	}
	QWidget::changeEvent(event);  // 调用基类的changeEvent
}

void CBeltCorrectMain::setStyleSheetFile()
{
	QString sQssPath = ":/qss/styleSheet.css";
	QFile qss(sQssPath);
	qss.open(QFile::ReadOnly);
	this->setStyleSheet(qss.readAll());
}

void CBeltCorrectMain::initConnect()
{
	connect(ui.pushButtonVideoPlay, &QPushButton::clicked, this, &CBeltCorrectMain::slotPushButtonVodeoPlay);
	connect(ui.pushButtonLarge, &QPushButton::clicked, this, &CBeltCorrectMain::slotPushButtonLarge);
	connect(ui.pushButtonSnap, &QPushButton::clicked, this, &CBeltCorrectMain::slotPushButtonSnap);
	connect(ui.pushButtonCorrectResult, &QPushButton::clicked, this, &CBeltCorrectMain::slotPushButtonCorrectResult);
	connect(ui.pushButtonCorrectPlc, &QPushButton::clicked, this, &CBeltCorrectMain::slotPushButtonCorrectPlc);
	connect(ui.pushButtonPointConfig, &QPushButton::clicked, this, &CBeltCorrectMain::slotPushButtonPointConfig);
}

void CBeltCorrectMain::initUi()
{
	QString sName = QString::fromLocal8Bit("皮带跑偏检测");
	setWindowTitle(sName);
	ui.labelName->setText(m_pDev->m_pDevData->m_sPointName);
	setContentsMargins(0, 0, 0, 0);
	setMinimumSize(1700, 900);
	setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
	this->setWindowState(Qt::WindowMaximized);
	ui.gridLayoutLog->addWidget(CLogOper::instance().createLogWidget(m_pDev->m_pDevData->m_pDevDataCam->m_sCamerName));
	loadCorrectState();				//加载跑偏状态界面
}

void CBeltCorrectMain::loadCorrectState()
{
	CDevDataBeltRealign* pDevDataBeltRealign = m_pDev->m_pDevData;
	QString sPointGuid = pDevDataBeltRealign->m_sPointGuid;
	m_pCorrectPointState = new CCorrectPointState(pDevDataBeltRealign);
	ui.gridLayout->addWidget(m_pCorrectPointState);
}

bool CBeltCorrectMain::startPlayVideo()
{
	SCtrInfo infoTx;
	infoTx.m_pLabel = ui.label;
	infoTx.m_hWid = ui.label->winId();
	infoTx.m_sGuid = m_pDev->m_sPointGuid;
	infoTx.m_nCtrCmd = enCRealPlay;
	infoTx.m_nCtrDevType = enSubDev1;
	infoTx.m_nSubCtrDevType = enCamer;
	bool bTxRet = m_pDev->ctrDev(infoTx);
	if (bTxRet == true)
	{
		m_bIsPlaying = true;
		ui.pushButtonVideoPlay->setIcon(QIcon(":/icon/StopPlay.png"));
	}
	return bTxRet;
}

bool CBeltCorrectMain::endPlayVideo()
{
	SCtrInfo infoTx;
	infoTx.m_pLabel = ui.label;
	infoTx.m_hWid = ui.label->winId();
	infoTx.m_sGuid = m_pDev->m_sPointGuid;
	infoTx.m_nCtrCmd = enCStopRealPlay;
	infoTx.m_nCtrDevType = enSubDev1;
	infoTx.m_nSubCtrDevType = enCamer;
	bool bTxRet = m_pDev->ctrDev(infoTx);
	if (bTxRet == true)
	{
		m_bIsPlaying = false;
		ui.pushButtonVideoPlay->setIcon(QIcon(":/icon/play.png"));
	}
	return bTxRet;
}

void CBeltCorrectMain::refreshData(bool bRefresh)
{
	if (bRefresh)
	{
		startPlayVideo();
	}
	else
	{
		endPlayVideo();
	}
	m_pCorrectPointState->refreshData(bRefresh);
}

void CBeltCorrectMain::slotPushButtonVodeoPlay()
{
	if (m_bIsPlaying)//如果正在播放视频
	{
		endPlayVideo();
		
	}
	else//则播放当前选中的节点
	{
		startPlayVideo();
		
	}
}

void CBeltCorrectMain::slotPushButtonLarge()
{
	CVideoLargeDlg oper(m_pDev, 0);
	oper.exec();
}

void CBeltCorrectMain::slotPushButtonSnap()
{
	if (!m_pDev->m_pDevData->m_pDevDataCam->m_bLogin)
		return;
	QString sImgUrl = m_pDev->m_pDevData->m_sRecordPath + "Image/";
	SCtrInfo infoTx;
	infoTx.m_sGuid = m_pDev->m_sPointGuid;
	infoTx.m_nCtrCmd = enCGetImage;
	infoTx.m_nCtrDevType = enSubDev1;
	infoTx.m_nSubCtrDevType = enCamer;
	infoTx.m_bCtrSync = true;
	infoTx.m_sImgUrl = sImgUrl;
	bool bTxRet = m_pDev->ctrDev(infoTx);
	if (bTxRet)
	{
		messageBox(0, A2T("图片抓拍成功，图片保存路径:%1").arg(infoTx.m_sImgUrl));
		return;
	}
	else
	{
		messageBox(1, A2T("图片抓拍失败"));
	}
}

void CBeltCorrectMain::slotPushButtonCorrectResult()
{
	CCorrectResult oper(m_pDev->m_sPointGuid);
	oper.exec();
}

void CBeltCorrectMain::slotPushButtonCorrectPlc()
{
	CCorrectPlcOper oper(m_pDev->m_sPointGuid, m_pDev);
	oper.exec();
}

void CBeltCorrectMain::slotPushButtonPointConfig()
{
	CCorrectDevCfgDlg oper(m_pDev->m_pDevData);
	oper.exec();
}

